#include "GraphicsSquare.h"
#include <QPainter>

qreal GraphicsSquare::SQUARE_WIDTH = 0;

GraphicsSquare::GraphicsSquare():x(0),y(0)
{
    sqType=UNDEFINED;
    m_rect = QRectF(0,0,SQUARE_WIDTH,SQUARE_WIDTH);
    setPos(x*SQUARE_WIDTH,y*SQUARE_WIDTH);
}

QRectF GraphicsSquare::boundingRect() const
{
    return m_rect;
}

void GraphicsSquare::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    setWidth(SQUARE_WIDTH);
    setPos(x*SQUARE_WIDTH,y*SQUARE_WIDTH);

    switch(sqType){
    case UNDEFINED:
        painter->setBrush(QBrush(Qt::black));break;
    case WALL:
        painter->setBrush(QBrush(QColor(150,150,150)));break;
    case ROAD:
        painter->setBrush(QBrush(Qt::white));break;

    }

    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->setPen(QPen(Qt::black));

    if(states.testFlag(FRONTIER)){
        painter->setBrush(QColor(128, 128, 255));
    }
    if(states.testFlag(TRACE)&&!states.testFlag(FRONTIER)){
        painter->setBrush(QColor(32, 255, 128));
    }
    if(states.testFlag(PATH)){
        painter->setBrush(QColor(255, 255, 0));
    }
    painter->drawRect(m_rect);

    if(states.testFlag(SOURCE)){
        painter->setBrush(QColor(255, 125, 125));
        painter->drawEllipse(QPointF(SQUARE_WIDTH/2,SQUARE_WIDTH/2),SQUARE_WIDTH/3,SQUARE_WIDTH/3);
    }
    if(states.testFlag(DESTINATION)){
        painter->setBrush(QColor(125, 255, 125));
        painter->drawEllipse(QPointF(SQUARE_WIDTH/2,SQUARE_WIDTH/2),SQUARE_WIDTH/3,SQUARE_WIDTH/3);
    }
    if(states.testFlag(CALCULATED)){
        if(states.testFlag(BEST)){
            QFont font;
            font.setPointSize(15);
            painter->setFont(font);
        }
        painter->drawText(m_rect,Qt::AlignCenter,QString::number(cost,'f',2));
    }

}
